const path = require('path');
const webpack = require('webpack');
const CompressionWebpackPlugin = require('compression-webpack-plugin');

function resolve (dir) {
    return path.join(__dirname, dir);
}

module.exports = {
    productionSourceMap: false,
	assetsDir: 'static',
    devServer: {
        port: 3001,
        host: '0.0.0.0',
        https: false,
        open: true,
        proxy: {
            '/api': {
                target: 'https://lzapi.shyunhua.com',
                changeOrigin: true,
                pathRewrite: {
                    '^/api': '/'
                }
            }
        }
    },
    configureWebpack: config => {
		// gzip压缩
		const productionGzipExtensions = ['html', 'js', 'css']
		config.plugins.push(
			new CompressionWebpackPlugin({
				filename: '[path].gz[query]',
				algorithm: 'gzip',
				test: new RegExp(
					'\\.(' + productionGzipExtensions.join('|') + ')$'
				),
				threshold: 10240, // 只有大小大于该值的资源会被处理 10240
				minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
				deleteOriginalAssets: false // 删除原文件
			})
		)
		// 公共代码抽离
		config.optimization = {
			splitChunks: {
				cacheGroups: {
					vendor: {
						chunks: 'all',
						test: /node_modules/,
						name: 'vendor',
						minChunks: 1,
						maxInitialRequests: 5,
						minSize: 0,
						priority: 100
					},
					common: {
						chunks: 'all',
						test: /[\\/]src[\\/]js[\\/]/,
						name: 'common',
						minChunks: 2,
						maxInitialRequests: 5,
						minSize: 0,
						priority: 60
					},
					styles: {
						name: 'styles',
						test: /\.(sa|sc|c)ss$/,
						chunks: 'all',
						enforce: true
					},
					runtimeChunk: {
						name: 'manifest'
					}
				}
			}
		}
    },
	css: {
	    loaderOptions: {
	      sass: {
	        prependData: `@import "./src/styles/global.scss";`
	       }
	    }
	  },
    lintOnSave: true,
    chainWebpack: (config)=>{
        config.entry('main').add('babel-polyfill'),
        config.resolve.alias
            .set('@$', resolve('src'))
            .set('components', resolve('src/components'))
            .set('assets',resolve('src/assets'))
            .set('styles',resolve('src/styles'))
            .set('api', resolve('src/api'))
            .set('utils', resolve('src/utils'))
            .set('store', resolve('src/store'))
    }
}
